package com.tz.service.impl;

import com.tz.entity.Poll;
import com.tz.entity.ResearchDetail;
import com.tz.entity.json.PollDetails;
import com.tz.mapper.ResearchDetailMapper;
import com.tz.service.ResearchDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/1/3 0003.
 */
@Transactional
@Service
public class ResearchDetailServiceImpl implements ResearchDetailService {

    @Autowired
    ResearchDetailMapper researchDetailMapper;

    @Override

    public void alterResearchDetails(List<Long> detailid) {

        researchDetailMapper.alterResearchDetails(detailid);
    }

    @Override
    public void addResearchDetail(Long researchid, ResearchDetail researchDetail) {
        researchDetail.setEffective(1);
        int time = 0;
        Double total = 0d;
        for (Poll p : researchDetail.getPolls()) {
            if (p.getAnswer().length() == 1) {
                time++;
                total += Double.parseDouble(p.getAnswer());
            }
        }
        researchDetail.setAver(total / time);
        researchDetailMapper.insertResearchDetail(researchid, researchDetail);
        researchDetailMapper.insertBatchPolls(researchDetail.getDetailid(), researchDetail.getPolls());
    }

    @Override
    public List<ResearchDetail> gainAllDetails(Long researchid) {
        return researchDetailMapper.selectDetailsByResearch(researchid);
    }

    @Override
    public ResearchDetail selectDetailById(Long detailid) {
        return researchDetailMapper.selectDetailById(detailid);
    }

    @Override
    public List<Poll> gainAllPolls(Long detailid) {

        return researchDetailMapper.selectAllPollsByDetailId(detailid);
    }

    @Override
    public void insertBatchPolls(Long detailid, List<Poll> polls) {

        researchDetailMapper.insertBatchPolls(detailid, polls);
    }

    @Override
    public Integer didThisResearch(Long researchid, String ipaddress) {
        return researchDetailMapper.selectDetailByIPandId(researchid, ipaddress);
    }

    @Override
    public Integer countDetailbyResearchId(Long researchid) {
        return researchDetailMapper.countDetailByResearchId(researchid);
    }

    @Override
    public List<PollDetails> getPollDetails(ResearchDetail researchDetail) {
        List<PollDetails> pollDetailList = new ArrayList<>();
        if (researchDetail != null) {
            List<Poll> polls = researchDetail.getPolls();
            System.out.println(polls);
            if (polls.size() > 0) {
                //定义一个set,用来存放类目
//                Set<String> stringSet = new HashSet<>();
                String temp = "";
                PollDetails details = new PollDetails();
                for (Poll poll : polls) {
                    System.out.println(poll.getQuestion());
                    String[] strings = poll.getQuestion().split(";");
                    /*判断是建议还是类目*/
                    if (strings.length > 1) {
                        if (!temp.equals(strings[0])) {
                            details = new PollDetails();
                            details.setTitle(strings[0]);
                            temp = strings[0];
                            pollDetailList.add(details);
                            details.setPoll(new ArrayList<>());
                        }
                        Poll poll11 = new Poll();
                        poll11.setQuestion(strings[1]);
                        poll11.setAnswer(poll.getAnswer());
                        details.getPoll().add(poll11);

                    } else {
                        PollDetails pollDetails = new PollDetails();
                        List<Poll> list = new ArrayList<>();
                        Poll poll11 = new Poll();
                        poll11.setQuestion(strings[0]);
                        poll11.setAnswer(poll.getAnswer());
                        list.add(poll11);
                        pollDetails.setPoll(list);
                        pollDetailList.add(pollDetails);
                    }
                }
                //设置pollDetail
//                    for (String s : stringSet) {
//                        //创建对象
//                        PollDetails pollDetails = new PollDetails();
//                        pollDetails.setTitle(s);
//                        pollDetailss.add(pollDetails);
//                    }
                //遍历对象集合
//                    for (PollDetails pollDetails : pollDetailss) {
//                        //再次遍历poll
//                        if (pollDetails.getTitle()!=null){
//                            List<Poll> ps = new ArrayList<>();
//                            for (Poll poll : polls) {
//                                String[] strings = poll.getQuestion().split(";");
//                        /*判断是建议还是类目*/
//                                if (strings.length > 1) {
//                                    //说明是类目
//                                    Poll poll1 = new Poll();
//                                    poll1.setQuestion(strings[1]);
//                                    poll1.setAnswer(poll.getAnswer());
//                                    ps.add(poll1);
//                                }
//                            }
//                            pollDetails.setPoll(ps);
//                        }
//                }
            }
        }
        return pollDetailList;
    }

    @Override
    public Double selectAVGByResearchID(Long researchid) {
        Double dob = researchDetailMapper.selectAVGByResearchID(researchid);
        return dob == null ? 0d : dob;
    }
}
